package com.genia.controller;

import com.genia.entity.User;
import com.genia.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String strId=req.getParameter("id");
        String strName=req.getParameter("name");
        String strEmail=req.getParameter("email");
        String strAge=req.getParameter("age");
        String strPwd=req.getParameter("pwd");


        //创建spring的容器对象
        String config="applicationContext.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(config);
        System.out.println("容器对象的信息========"+ac);
        UserService service = (UserService)ac.getBean("UserService");

        User user=new User();
        user.setId(Integer.parseInt(strId));
        user.setName(strName);
        user.setEmail(strEmail);
        user.setAge(Integer.parseInt(strAge));
        user.setPwd(strPwd);

        service.addUser(user);
        //给一个jsp页面
        req.getRequestDispatcher("/result.jsp").forward(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
